﻿@{
    ViewBag.Title = "订单详情";
    Layout = "~/Views/Shared/_layoutPersonal.cshtml";
}
@Styles.Render("~/Content/css/orderInfo")
<article class="main">
    <nav class="mod_nav">
        <div class="lay_left">
            <a class="nav_back" onclick="turnBack()"><img src="/images/return_1.png" ontouchstart="replaceBackImage(this, 1)" ontouchend="replaceBackImage(this, 2)"></a>
            <span>订单详情</span>
        </div>
    </nav>
    <section class="mod_order">
        <div class="orderInfo">
            <ul>
                <li><span id="time"></span><span id="orderid"></span></li>
                <li class="userInfo">
                    <span class="terminalName"></span>
                    <span class="terminalAddr"></span>
                </li>
                <li id="payStatusHelper" class="payhelper"><span id="todoStatus"></span><span id="goodsCodeHelper" class="codehelper">取货码</span></li>
            </ul>
        </div>
        <div class="orderList">
            <ul id="orderListInfo"></ul>
        </div>
        <div class="priceHelper"><span id="payStatus"></span><span id="price"></span></div>
    </section>
    <section class="mod_control">
        <span class="cost"></span>
        <div class="btn_list">
            <a class="btn-quite" onclick="cancelOrder()">取消订单</a>
            <a class="btn-pay"  onclick="PayOrder()">去支付</a>
        </div>
    </section>
</article>
<script>
    var isPaying = false;

    function turnBack() {
        OrderMgr.resetOrderChangeNotifyCallback();
        OrderMgr.resetPickedGoodsSucNotifyCallback();
        OrderMgr.resetPickedGoodsErrorNotifyCallback();
        OrderMgr.resetOrderGoodsChangeNotifyCallback();
        window.location.href = '/OrderMgr/Index';
    }

    window.onload=function () {
        OrderMgr.setOrderChangeNotifyCallback(orderTakeOffCallback);
        OrderMgr.setPickedGoodsSucNotifyCallback(pickedSucGoods);
        OrderMgr.setPickedGoodsErrorNotifyCallback(pickedErrorGoods);
        OrderMgr.setOrderGoodsChangeNotifyCallback(pickedSucGoods);
        showOrderDetails();
    }

    function showOrderDetails() {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        if (null != curOrderInfo) {
            displayDetails(curOrderInfo);
        }
    }

    function getOrderPayStatus(orderInfo, totalPrice) {
        if (OrderMgr.failPayOrderType == orderInfo.order_type) {
            $(".btn_list").show();
            $("#payStatusHelper").show();
            $("#todoStatus").text("请支付后到货机位置取货");
            $("#goodsCodeHelper").hide();
            $("#payStatus").text("未支付");
            $("#price").text("￥" + totalPrice.toFixed(1));
        } else if (OrderMgr.unpickedOrderType == orderInfo.order_type) {
            $(".mod_control").hide();
            $("#payStatusHelper").show();
            $("#todoStatus").text("请尽快到货机位置取货");
            if (orderInfo.point_info.vender_pick_type == 1) {
                $("#goodsCodeHelper").show();
            } else {
                $("#goodsCodeHelper").hide();
            }

            $("#payStatus").text("已支付");
            $("#payStatus").attr("style", "color:#000000");
            $("#price").text("￥" + totalPrice.toFixed(1));
            $("#price").attr("style", "color:#000000");
        } else if (OrderMgr.pickedOrderType == orderInfo.order_type) {
            $(".mod_control").hide();
            $("#payStatusHelper").hide();
            $("#payStatus").text("已取货");
            $("#payStatus").attr("style", "color:#000000");
            $("#price").text("￥" + totalPrice.toFixed(1));
            $("#price").attr("style", "color:#000000");
        }
    }

    function displayDetails(orderInfo) {
        $("#time").text(orderInfo.time);
        if (orderInfo.order_type == OrderMgr.failPayOrderType) {
            $("#orderid").text("订单号： 暂无");
        } else {
            $("#orderid").text("订单号： " + orderInfo.orderid);
        }
        $(".terminalName").text(orderInfo.point_info.name);
        $(".terminalAddr").text(orderInfo.point_info.address);
        var totalFee = OrderMgr.getTotalFee(orderInfo);
        getOrderPayStatus(orderInfo, totalFee);

        showGoodsList(orderInfo);
    }

    function showGoodsList(orderInfo) {
        var url = '/OrderInfo/ShowGoodsList' + '?' + 'orderType=' + Number(orderInfo.order_type) + '&pickType=' + Number(orderInfo.point_info.vender_pick_type);
        NativeAjax(orderInfo, url, function (msg) {
            loadPartialViewResult(msg, $("#orderListInfo"), 1);
        });
    }

    function cancelOrderCallback() {
        turnBack();
    }

    function cancelOrder() {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        OrderMgr.cancelOrder(curOrderInfo, cancelOrderCallback);
    }

    function validOrderCallback(orderPayInfo) {
        var orderType = orderPayInfo.pay_type;
        if (orderType == OrderMgr.jifenPayType) {//积分支付
            window.onload.herf = '/OrderInfo/Index';
        } else if (orderType == OrderMgr.weixinPayType) {//微信支付
            order_info_tag_json = JSON.parse(orderPayInfo.order_pay_info.order_info_tag);
            var totleFee = order_info_tag_json.total_fee;
            var userOrderNumber = order_info_tag_json.attach;
            var attachInfo = totleFee + "|" + userOrderNumber;//这里可以带信息去微信服务器 支付成功后会原样返回
            window.location.href = '/TenPayV3/Index?attachInfo=' + attachInfo
                + '&orderNumber=' + orderPayInfo.order_pay_info.order_id + "&showwxpaytitle=1";
        }
    }
    function invalidOrderCallback() {
        //这里表示是无效订单，返回订单列表
        isPaying = false;
        turnBack();
    }

    function PayOrder() {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        if (null != curOrderInfo) {
            if (isPaying == false) {
                isPaying = true;
                //目前只支持微信支付，把所有的支付方式都改成微信支付
                curOrderInfo.pay_type = OrderMgr.weixinPayType;
                OrderMgr.orderRepayHandle(curOrderInfo, validOrderCallback, invalidOrderCallback);
            }
        } else {
            venderDialog("订单信息错误，请重试！");
            turnBack();
        }
    }

    function orderTakeOffCallback(orderInfo) {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        if ((null != curOrderInfo) && (curOrderInfo.orderid == orderInfo.orderid)) {
            var totalFee = OrderMgr.getTotalFee(orderInfo);
            getOrderPayStatus(orderInfo, totalFee);
            showGoodsList(orderInfo);
        }
    }

    var AKEYPICKEDDIALOG = null;
    function aKeyPickGoods(goodsId, goodsCode) {
        var orderInfo = OrderMgr.getCurOrderInfo();
        var goodsList = orderInfo.goods_info;
        var goodsInfo = null;
        for (var i = 0; i < goodsList.length; i++) {
            if (goodsList[i].goods_info.goodsid == goodsId) {
                goodsInfo = goodsList[i].goods_info;
                break;
            }
        }

        if (null == goodsInfo) {
            return;
        }

        var html = '<div class="dialogBody">'
        + '<div class="dialogContent">'
        + '<div class="aKeyPickedTitle">确认收货信息</div>'
        + '<img class="aKeyPickedGoodsImg" src=\"' + goodsInfo.image + '\">'
        + '<div class="terminalName">'
        + orderInfo.point_info.name
        + '</div>'
        + '<div class="terminalAddress">'
        + orderInfo.point_info.address
        + '</div>'
        + '</div>'
        + '<div class="dialogFooter">'
        + '<div class="cancelPicked" onclick="cancelPicked()">取消</div>'
        + '<div class="Picked" onclick="pickedGoods(\'' + goodsId + '\', \'' + goodsCode + '\')">出货</div>'
        + '</div>'
        + '</div>';
        AKEYPICKEDDIALOG = dialog({
            id: "pickedBeginDialog",
            content: html,
            opacity: 0
        });
        AKEYPICKEDDIALOG.showModal();
    }

    function closeAKEYPICKEDDIALOG() {
        if (null != AKEYPICKEDDIALOG) {
            AKEYPICKEDDIALOG.close().remove();
            AKEYPICKEDDIALOG = null;
        }
    }

    function cancelPicked() {
        closeAKEYPICKEDDIALOG();
    }
    function pickedGoods(goodsId, goodsCode) {
        OrderMgr.setPickedGoodsSucNotifyCallback(function (orderInfo) {
            pickedSucGoods(orderInfo);
        });
        OrderMgr.setPickedGoodsErrorNotifyCallback(function (msg) {
            pickedErrorGoods(msg);
        });
     
        OrderMgr.aKeyPickGoods(goodsId, goodsCode);
        closeAKEYPICKEDDIALOG();
        var html = '<div class="dialogBody">'
        + '<div class="dialogContent">'
        + '<img class="pickingAnim" src="/images/pickgoods.gif">'
        + '<div class="pickingGoods">正在出货</div>'
        + '</div>'
        + '</div>'
        AKEYPICKEDDIALOG = dialog({
            id: "pickingDialog",
            content: html,
            opacity: 0
        });
        AKEYPICKEDDIALOG.showModal();
    }
    function pickedErrorGoods(errorMsg) {
        closeAKEYPICKEDDIALOG();
        var html = '<div class="dialogBody">'
        + '<div class="dialogContent">'
        + '<img class="pickedError" src="/images/pick_error.png">'
        + '<div class="terminalName">出货失败</div>'
        + '<div class="terminalAddress">'
        + errorMsg
        + '</div>'
        + '<div class="dialogFooter">'
        + '<div class="pickedErrorBtn" onclick="pickedError()">确定</div>'
        + '</div>'
        + '</div>'
        AKEYPICKEDDIALOG = dialog({
            id: "pickedErrorDialog",
            content: html,
            opacity: 0
        });
        AKEYPICKEDDIALOG.showModal();
    }

    function pickedError() {
        closeAKEYPICKEDDIALOG();
    }

    function pickedSucGoods(orderInfo) {
        closeAKEYPICKEDDIALOG();
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        if ((null != curOrderInfo) && (curOrderInfo.orderid == orderInfo.orderid)) {
            showGoodsList(orderInfo);
        }
    }

</script>
